package cn.cnlee.app.repository;

import cn.cnlee.app.pojo.LoginDTO;
import cn.cnlee.app.pojo.LoginVo;
import cn.cnlee.app.remote.response.ResponseTransformer;
import cn.cnlee.app.remote.rx.RxSubscriber;
import cn.cnlee.app.util.StringUtil;
import cn.cnlee.arch.rx.RxSchedulers;
import cn.cnlee.arch.util.Logger;
import cn.cnlee.arch.view.stateview.StateConstants;

public class UserRepository extends BaseRepository {
    protected final static String TAG = UserRepository.class.getSimpleName();
    public static String EVENT_KEY_LOGIN = null;

    public UserRepository() {
        EVENT_KEY_LOGIN = StringUtil.sBlank(EVENT_KEY_LOGIN, StringUtil.createEventKey());
    }

    public void login(LoginVo loginVo) {
        addDisposable(apiService.login(loginVo)
                .compose(ResponseTransformer.handleFlowableResult())
                .compose(RxSchedulers.io_main())
                .subscribeWith(new RxSubscriber<LoginDTO>() {

                    @Override
                    protected void showLoading() {
                        postState(StateConstants.LOADING_STATE);
                    }

                    @Override
                    protected void onNoNetWork() {
                        super.onNoNetWork();
                        postState(StateConstants.NET_WORK_STATE);
                    }

                    @Override
                    public void onSuccess(LoginDTO loginDTO) {
                        postData(EVENT_KEY_LOGIN, loginDTO);
                        postState(StateConstants.SUCCESS_STATE);
                    }

                    @Override
                    public void onFailure(String msg, int code) {
                        postState(StateConstants.ERROR_STATE);
                    }

                }));
    }
}
